const Controllers = require('../../controllers/Controller.js').controller;
const util = require('../../../utils/util.js');
const dateTimePicker = require('../../../utils/dateTimePicker.js');
Page({
  data: {
    name:'',
    image:'',
    price:'',
    haveGivePrice:0,
    haveGiveIntegral:0,
    givePrice:'',
    giveIntegral:'',
    switch1Checked:false,
    joinProduct:[],
    type:1,
    imagebool:false,
    timeState:0,
    startTime:'请选择时间',
    endTime:'请选择时间',
    desc:'',
    haveGoods:1,
    currentDate:util.formatTime_1(new Date()),
    isloading:false
  },
  onLoad: function (options) {
    // var DATE = formatTime(new Date(),'-');
    // this.setData({
    //   startTime:DATE
    // })
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj1 = dateTimePicker.dateTimePicker();
    var obj2 = dateTimePicker.dateTimePicker();
    this.setData({
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
      dateTimeArray2: obj2.dateTimeArray,
      dateTime2: obj2.dateTime,
    });
  },
  onShow(){
    var chooseProduct=this.data.chooseProduct
    var all=this.data.joinProduct
    all.forEach(function(item,index){
      chooseProduct.forEach(function(item1,index1){
        if(item.id==item1.id){
          chooseProduct.splice(index1,1)
        }
      })
    })
    if(chooseProduct){
      for(let item of chooseProduct){
        item.num=1
      }
      this.setData({
        joinProduct:all.concat(chooseProduct)
      })
    }
  },
  changeDateTime1(e) {
    var date=this.data.dateTimeArray1
    var time=e.detail.value
     this.setData({
      dateTime1: time,
      startTime:date[0][time[0]]+"-"+date[1][time[1]]+"-"+date[2][time[2]]+" "+date[3][time[3]]+":"+date[4][time[4]]
     });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr,
    });
  },
  changeDateTime2(e) {
    var date=this.data.dateTimeArray2
    var time=e.detail.value
     this.setData({
      dateTime2: time,
      endTime:date[0][time[0]]+"-"+date[1][time[1]]+"-"+date[2][time[2]]+" "+date[3][time[3]]+":"+date[4][time[4]]
     });
  },
  changeDateTimeColumn2(e) {
    var arr = this.data.dateTime2, dateArr = this.data.dateTimeArray2;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      dateTimeArray2: dateArr,
      dateTime2: arr,
    });
  },
  setimage(e){
    var type=e.currentTarget.dataset.type;
    if(type==0){
      this.setData({
        imagebool:false,
      })
    }else{
      this.setData({
        imagebool:true
      })
    }
  },
  deleteimg(){
    this.setData({
      image:''
    })
  },
  choose(){
    let that=this;
    wx.chooseImage({
      success (res) {
        const tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://www.xlshhr.com/beauty-industry/api/img/uploadFile', 
          filePath: tempFilePaths[0],
          name: 'image',
          formData: {
            'Content-Type': 'application/form-data',
            'sessionId': wx.getStorageSync('token')
          },
          success (res){
              console.log(res);
              let src=JSON.parse(res.data)
             that.setData({
               image:src.data
             })
          }
        })
      }
    })
  },
  switchChange(e){
    var type=e.currentTarget.dataset.type;
    if(type==1){
      if(e.detail.value){
        this.setData({
          haveGivePrice:1,
          // haveGiveIntegral:0,
          // haveGoods:0
        })
      }else{
        this.setData({
          haveGivePrice:0,
          // haveGiveIntegral:0,
          // haveGoods:0
        })
      }
    }
    if(type==2){
      if(e.detail.value){
        this.setData({
          // haveGivePrice:0,
          haveGiveIntegral:1,
          // haveGoods:0
        })
      }else{
        this.setData({
          // haveGivePrice:0,
          haveGiveIntegral:0,
          // haveGoods:0
        })
      }
    }
    if(type==3){
      if(e.detail.value){
        this.setData({
          // haveGivePrice:0,
          // haveGiveIntegral:0,
          haveGoods:1
        })
      }else{
        this.setData({
          // haveGivePrice:0,
          // haveGiveIntegral:0,
          haveGoods:0
        })
      }
    }
  },
  addgoods(e){
    let nowListSting = JSON.stringify(this.data.joinProduct);
    this.setData({
      // haveGivePrice:0,
      // haveGiveIntegral:0,
      haveGoods:1
    })
    wx.navigateTo({
      url: '/pages/settlement/AddGoods/AddGoods?nowList=' + nowListSting,
    })
  },
  cancel(e){
    wx.navigateBack({
      delta: 1
    })
  },
  delete(e){
    this.data.joinProduct.splice(e.currentTarget.dataset.index,1)
    this.setData({
      joinProduct:this.data.joinProduct
    })
  },
  plus(e){
    var index=e.currentTarget.dataset.index
    this.data.joinProduct[index].num++;
    this.setData({
      joinProduct:this.data.joinProduct
    })
  },
  mins(e){
    var index=e.currentTarget.dataset.index
    if(this.data.joinProduct[index].num>1){
      this.data.joinProduct[index].num--;
      this.setData({
        joinProduct:this.data.joinProduct
      })
    }
  },
  changenum(e){
    var index=e.currentTarget.dataset.index
    if(this.data.joinProduct[index].num<1){
      this.data.joinProduct[index].num=1
      this.setData({
        joinProduct:this.data.joinProduct
      })
    }else{
      this.data.joinProduct[index].num=e.detail.value
      this.setData({
        joinProduct:this.data.joinProduct
      })
    }
  },
  changeinput(e){
    var name=e.currentTarget.dataset.name;
    var val=e.detail.value;
    this.setData({
        [name]:val || ''
    })
  },
  vipsave(){
    if(!this.data.name){
      wx.showToast({
        title: '请输入活动名称',
        icon: 'none',
        duration: 2000
      })
      return
  }
  if(!this.data.price){
    wx.showToast({
      title: '请输入会员卡价格',
      icon: 'none',
      duration: 2000
    })
    return
  }
    if(this.data.haveGivePrice==1 && !this.data.givePrice){
        wx.showToast({
          title: '请输入赠送金额',
          icon: 'none',
          duration: 2000
        })
        return
    }
    if(this.data.haveGiveIntegral==1 && !this.data.giveIntegral){
        wx.showToast({
          title: '请输入赠送积分',
          icon: 'none',
          duration: 2000
        })
        return
    }
    if(this.data.startTime && this.data.endTime){
      var currentdate = Date.parse(util.formatTime_1(new Date()));
      let startTime=Date.parse(new Date(this.data.startTime.replace(/-/g, '/')));
      let endTime=Date.parse(new Date(this.data.endTime.replace(/-/g, '/')));
      if(startTime>=endTime){
        wx.showToast({
          title: '开始时间不能少于结束时间',
          icon: 'none',
          duration: 2000
        })
        return
      }
      if(currentdate>startTime){
        wx.showToast({
          title: '开始时间不能少于当前时间',
          icon: 'none',
          duration: 2000
        })
        return
      }
      if(currentdate>endTime){
        wx.showToast({
          title: '结束时间不能少于当前时间',
          icon: 'none',
          duration: 2000
        })
        return
      }
    }
    if(!this.data.price){
        wx.showToast({
          title: '请输入会员卡价格',
          icon: 'none',
          duration: 2000
        })
        return
    }
    if(this.data.timeState==1 && !this.data.effectiveTime){
        wx.showToast({
          title: '请输入有效月数',
          icon: 'none',
          duration: 2000
        })
        return
    }
    let data={};
    data.shopId=wx.getStorageSync('shopId');
    data.name=this.data.name;
    if(this.data.imagebool){
      data.image=this.data.image || '';
    }
    data.timeState=this.data.timeState;
    data.startTime=this.data.startTime=='请选择时间'?'':this.data.startTime;
    data.endTime=this.data.endTime=='请选择时间'?'':this.data.endTime;
    data.price=this.data.price;
    data.type=this.data.type;
    data.desc=this.data.desc || '';
    data.haveGivePrice=this.data.haveGivePrice;
    data.givePrice=this.data.haveGivePrice==1?this.data.givePrice:'';
    data.haveGiveIntegral=this.data.haveGiveIntegral;
    data.haveGoods=this.data.haveGoods;
    data.giveIntegral=this.data.haveGiveIntegral==1?this.data.giveIntegral:'';
    if(this.data.joinProduct.length>0 && this.data.haveGoods==1){
      let goodsIdNum=[]
      for(let item of this.data.joinProduct){
        goodsIdNum.push(item.id+'-'+item.num);
      }
      data.goodsIdNum=goodsIdNum.join(',')
    }
    if(this.data.isloading){
      return
    }
    this.setData({
      isloading:true
    })
    Controllers.vipsave(data).then(res => {
      wx.showToast({
        title: '添加成功！',
        icon: 'success',
        duration: 2000
      })
      var that=this
      setTimeout(res => {
        wx.navigateBack({ //返回
          delta: 1
        })
        that.setData({
        isloading:false
       })
      },2000)
    }).catch(()=>{
      this.setData({
        isloading:false
      })
    })
  },
})
